package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.config.VerifyCodeProperties;
import cn.itsource.ymcc.dto.CodeDto;
import cn.itsource.ymcc.exception.CustomException;
import cn.itsource.ymcc.exception.ErrorCode;
import cn.itsource.ymcc.service.VerifycodeService;
import cn.itsource.ymcc.util.AssertUtil;
import cn.itsource.ymcc.util.StrUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import static cn.itsource.ymcc.constant.ReidsConstant.SMS_CODE_KEY;

@Service
@Configuration
public class VerifycodeServiceImpl implements VerifycodeService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private VerifyCodeProperties verifCodeConfig;



    public void sendSmsCode_old(String phone) {
        //1. 生成短信验证码 -- > str工具 code
        String code = "";

        //2. 保存短信验证码到redis
        //2.1 业务key (smsCode:手机号)
        String key = "smsCode:" + phone;

        //3 判断Redis中是否有 code
        String result = (String)redisTemplate.opsForValue().get(key);
        if (result != null) {
            //3.1 如果有，判断是否过期 , 是不是超过60s
            String[] split = result.split(":");
            code = split[0];
            String time = split[1];
            if (System.currentTimeMillis() - Long.parseLong(time) < 60000) {
                //没有超过60s
                throw new CustomException(ErrorCode.TIME_NOT_ARRIVED);
            }
        } else {
            //如果没有，生成短信验证码，保存到redis
            code = StrUtils.getRandomString(6);
        }
        redisTemplate.opsForValue().set(key,code+":"+System.currentTimeMillis(),5, TimeUnit.MINUTES);

        //4. 发送短信验证码
        System.out.println("短信验证码发送成功! "+ code);
    }

    @Override
    public void sendSmsCode(String phone) {
        //1. 生成短信验证码 -- > str工具 code
        String code = "";

        //2. 保存短信验证码到redis
        //2.1 业务key (smsCode:手机号)
        String key = String.format(SMS_CODE_KEY, phone);

        //3 判断Redis中是否有 code
        CodeDto codeDto = (CodeDto)redisTemplate.opsForValue().get(key);
        if (codeDto != null) {
            //3.1 如果有，判断是否过期 , 是不是超过60s
            code = codeDto.getCode();
            Long time = codeDto.getDate().getTime();

            //断言工具优化 if 判断是否过期
            boolean freqTran = System.currentTimeMillis() - time < verifCodeConfig.getArrivedTime(); //我希望>60000 false
            AssertUtil.isFalse(freqTran,ErrorCode.TIME_NOT_ARRIVED);

        } else {
            //如果没有，生成短信验证码，保存到redis
            code = StrUtils.getRandomString(verifCodeConfig.getCodeLength());
        }
        redisTemplate.opsForValue().set(key,new CodeDto(code,new Date()),verifCodeConfig.getTimeout(), TimeUnit.MINUTES);

        //4. 发送短信验证码
        System.out.println("短信验证码发送成功! "+ code);
    }
}
